/********************************************************************************
 * Copyright (c) 2020 [Open Lowcode SAS](https://openlowcode.com/)
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0 .
 *
 * SPDX-License-Identifier: EPL-2.0
 ********************************************************************************/

package org.openlowcode.server.data.properties;

import java.util.ArrayList;

import org.openlowcode.module.system.data.choice.ApplocaleChoiceDefinition;
import org.openlowcode.server.data.ChoiceValue;
import org.openlowcode.server.data.DataObject;
import org.openlowcode.server.data.DataObjectDefinition;
import org.openlowcode.server.data.DataObjectElement;
import org.openlowcode.server.data.DataObjectPayload;
import org.openlowcode.server.data.DataObjectPropertyDefinition;
import org.openlowcode.server.data.PropertyExtractor;
import org.openlowcode.server.data.loader.FlatFileLoaderColumn;
import org.openlowcode.server.data.specificstorage.ExternalFieldSchema;
import org.openlowcode.server.data.storage.QueryCondition;
import org.openlowcode.server.data.storage.QueryOperatorEqual;
import org.openlowcode.server.data.storage.SimpleQueryCondition;
import org.openlowcode.server.data.storage.StoredTableIndex;
import org.openlowcode.server.data.storage.StringStoredField;
import org.openlowcode.server.data.storage.TableAlias;

/**
 * definition of the Hasid property
 * 
 * @author <a href="https://openlowcode.com/" rel="nofollow">Open Lowcode
 *         SAS</a>
 *
 * @param <E> type of data object
 */
public class HasidDefinition<E extends DataObject<E>>
		extends
		DataObjectPropertyDefinition<E> {
	private StringStoredField id;
	private StringStoredField deleted;
	/**
	 * creates a new definition of a Hasid property for a class of data
	 * object
	 * 
	 * @param parentobject parent object definition for the property
	 */
	public HasidDefinition(DataObjectDefinition<E> parentobject) {
		super(parentobject, "HASID");
		id = new StringStoredField("ID", null, 200);
		this.addFieldSchema(id);
		StoredTableIndex idindex = new StoredTableIndex("ID");
		idindex.addStoredFieldSchema(id);
		this.addIndex(idindex);
		deleted = new StringStoredField("DELETED",null,3,"N");
		this.addFieldSchema(deleted);
		StoredTableIndex deletedindex = new StoredTableIndex("DELETED");
		deletedindex.addStoredFieldSchema(deleted);
		this.addIndex(deletedindex);
	}
	
	@Override
	public FieldSchemaForDisplay<E>[] setFieldSchemaToDisplay() {
		@SuppressWarnings("unchecked")
		FieldSchemaForDisplay<E>[] returnvalue = new FieldSchemaForDisplay[2];
		returnvalue[0] = new FieldSchemaForDisplay<E>("Object Id", "the technical id generated by the system", id,
				false, true, -70, 20, this.parentobject);
		returnvalue[1] = new FieldSchemaForDisplay<E>("Deleted", "flag indicating if the line is current or deleted", deleted,
				false, true, -900, 15, this.parentobject);
		return returnvalue;
	}

	@Override
	public ArrayList<ExternalFieldSchema<?>> generateExternalSchema() {
		return null;
	}

	@Override
	public QueryCondition getUniversalQueryCondition(String aliasstring) {
		TableAlias alias = parentobject.getAlias(aliasstring);
		QueryCondition notdeletedcondition = new SimpleQueryCondition<String>(alias, deleted,
				new QueryOperatorEqual<String>(), "N");
		return notdeletedcondition;
	}

	@Override
	public FlatFileLoaderColumn<E> getFlatFileLoaderColumn(
			DataObjectDefinition<E> objectdefinition,
			String[] columnattributes,
			PropertyExtractor<E> propertyextractor,
			ChoiceValue<ApplocaleChoiceDefinition> locale) {
		// in the future, there may be a loader for technical id
		throw new RuntimeException("No loader for this property");
	}

	@Override
	public String[] getLoaderFieldList() {
		return new String[0];
	}

	@Override
	public String[] getLoaderFieldSample(String name) {
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DataObjectElement initiateFieldInstance(
			DataObjectPayload parentpayload) {
		return new Hasid<E>(this, parentpayload);
	}

}
